<?php
/**
 * Created by PhpStorm.
 * User: zyw
 * Date: 2018-02-25
 * Time: 16:39
 */
class Post extends MY_Controller{

    public function __construct(){
        parent::__construct();
    }
    /**
     * 获取帖子审核列表
     * name: index
     * @Author:taiyang
     */
    public function index(){
        $status=$this->input->get_post('status')?$this->input->get_post('status'):1;
        $page=$this->input->get_post('page')?$this->input->get_post('page'):1;
        $pagesize=$this->input->get_post('pagesize')?$this->input->get_post('pagesize'):10;
        $list=$this->PostModel->getPostList($status,$page,$pagesize);
        $num=$this->PostModel->getPostListNum($status);
        foreach ($list as $k=>$v){
            $list_data[]=$this->PostModel->get_post_data($v);
        }
        //取出所有的话题
        $topic_list=$this->TopicModel->getTopicList();
        //取出所有的标签
        $tag_list=$this->TagModel->getTagList();
        $new_tag=array_column($tag_list,'title','id');
        $new_topic=array_column($topic_list,'title','id');


//        print_r($list_data);
        //分页
        $url = 'index.php?c=post&m=index&status='.$status;
        $multipage = multipage($num, $page, $pagesize, $url);
        $pageTitle = "审核";
        $this->load->view('post/index',array(
            "pageTitle" => $pageTitle,
            "status_s" => $status,
            "list" => $list_data,
            "tag_list" => $tag_list,
            "topic_list" => $topic_list,
            "tag_list_json" => json_encode($tag_list),
            "topic_list_json" => json_encode($topic_list),
            "multipage" => $multipage,
            "new_topic" => $new_topic,
            "new_tag" => $new_tag,
        ));
    }

    /**
     * 帖子列表
     * name: post_list
     * @throws Exception
     * @Author:taiyang
     */
    public function post_list(){
        $img_w=$this->input->get_post('img_w')?$this->input->get_post('img_w'):"";//图片高度
        $img_h=$this->input->get_post('img_h')?$this->input->get_post('img_h'):"";//图片宽度
        $img_size=$this->input->get_post('img_size')?$this->input->get_post('img_size'):"";//图片大小
        $festival=$this->input->get_post('festival')?$this->input->get_post('festival'):"";//是否为节日
        $s_type_id=$this->input->get_post('s_type_id')?$this->input->get_post('s_type_id'):1;
        $s_topic=$this->input->get_post('s_topic')?$this->input->get_post('s_topic'):'';
        $add_time_e=$this->input->get_post('add_time_e')?$this->input->get_post('add_time_e'):'';
        $add_time_s=$this->input->get_post('add_time_s')?$this->input->get_post('add_time_s'):'';
        $use_time_e=$this->input->get_post('use_time_e')?$this->input->get_post('use_time_e'):'';
        $use_time_s=$this->input->get_post('use_time_s')?$this->input->get_post('use_time_s'):'';
        $s_content=$this->input->get_post('s_content')?$this->input->get_post('s_content'):'';
        $status=$this->input->get_post('status')?$this->input->get_post('status'):"";
        $is_hot=$this->input->get_post('is_hot')?$this->input->get_post('is_hot'):"";
        $post_type=$this->input->get_post('post_type')?$this->input->get_post('post_type'):"";
        $is_audit=$this->input->get_post('is_audit')?$this->input->get_post('is_audit'):"";
        $uid=$this->input->get_post('uid')?$this->input->get_post('uid'):'';
        $s_topic_id=$this->input->get_post('s_topic_id')?$this->input->get_post('s_topic_id'):'';
        $is_pic=$this->input->get_post('is_pic')?$this->input->get_post('is_pic'):'';
        $s_tag_id=$this->input->get_post('s_tag_id')?$this->input->get_post('s_tag_id'):'';
        $page=$this->input->get_post('page')?$this->input->get_post('page'):1;
        $pagesize=$this->input->get_post('pagesize')?$this->input->get_post('pagesize'):10;
        $res_order=$this->input->get_post('res_order')?$this->input->get_post('res_order'):6;
        $data=array(//,$status = '',$uid='',$topic='',$tag=''
            'post_type'=>$post_type,
            'status'=>$status,
            'is_audit'=>$is_audit,
            'uid'=>$uid,
            'topic'=>$s_topic_id,
            'tag'=>$s_tag_id,
            'is_pic'=>$is_pic,
            'is_hot'=>$is_hot,
            's_type_id'=>$s_type_id,
            's_content'=>$s_content,
            's_topic'=>$s_topic,
            'add_time_e'=>$add_time_e,
            'add_time_s'=>$add_time_s,
            'use_time_e'=>$use_time_e,
            'use_time_s'=>$use_time_s,
            'festival'=>$festival,
            'img_w'=>$img_w,
            'img_h'=>$img_h,
            'img_size'=>$img_size,
            'res_order'=>$res_order,

        );
        //取出所有的数据来源
        $source_res=$this->PostModel->get_source();


        $list=$this->PostModel->getPostAll($data,$page,$pagesize,$res_order);
        $num=$this->PostModel->getPostAllNum($data);
        foreach ($list as $k=>$v){
            $list_data[]=$this->PostModel->get_post_data($v);
        }
//        print_r($list_data);
        //取出所有的话题
        $topic_list=$this->TopicModel->getTopicList();
        $new_topic=array_column($topic_list,'title','id');

        //取出所有一级话题
        $topic_list_one=$this->TopicModel->querylist(array('parent_id'=>0));
        //取出所有的标签
        $tag_list=$this->TagModel->getTagList();
        $new_tag=array_column($tag_list,'title','id');

        //获取管理员操作人
        $admin_list=$this->UserAdmin->getAdminList();

//        print_r($list_data);
        //分页
//        $url = 'index.php?c=post&m=post_list&status='.$status."&uid=".$uid."&s_topic_id=".$s_topic_id."&s_tag_id=".$s_tag_id."&is_pic=".$is_pic;
        $url = 'index.php?c=post&m=post_list&status='.$status."&uid=".$uid."&s_topic_id=".$s_topic_id."&s_tag_id=".$s_tag_id."&is_pic=".$is_pic.'&s_type_id='.$s_type_id."&s_content=".$s_content.'&add_time_e='.$add_time_e.'&add_time_s='.$add_time_s.'&use_time_e='.$use_time_e.'&use_time_s='.$use_time_s.'&is_hot='.$is_hot.'&post_type='.$post_type.'&is_audit='.$is_audit.'&s_topic='.$s_topic.'&festival='.$festival.'&img_w='.$img_w.'&img_h='.$img_h.'&img_size='.$img_size.'&res_order='.$res_order;
        $multipage = multipage($num, $page, $pagesize, $url);
        $pageTitle = "帖子列表";
        $tag_arr=$this->TagModel->query_one(array("id"=>$s_tag_id));
        $topic_arr=$this->TopicModel->query_one(array("id"=>$s_topic_id));
        $this->load->view('post/post_list',array(
            "admin_list" => $admin_list,
            "pageTitle" => $pageTitle,
            "is_pic" => $is_pic,
            "topic_id" => $s_topic_id,
            "is_hot" => $is_hot,
            "uid" => $uid,
            "tag_arr" => !empty($s_tag_id)?$tag_arr:'',
            "topic_arr" => !empty($s_topic_id)?$topic_arr:'',
            'status'=>$status,
            'is_audit'=>$is_audit,
            "list" => $list_data,
            "tag_list_json" => json_encode($tag_list),
            "tag_list" => $tag_list,
            "topic_list_one" => $topic_list_one,
            "topic_list_json" => json_encode($topic_list),
            "topic_list" => $topic_list,
            "multipage" => $multipage,
            "s_type_id" => $s_type_id,
            "source_res" => $source_res,
            "post_type" => $post_type,
            "s_topic" => $s_topic,
            "s_content" => $s_content,
            "festival" => $festival,
            "img_w" => $img_w,
            "img_h" => $img_h,
            "img_size" => $img_size,
            "new_topic" => $new_topic,
            "new_tag" => $new_tag,
            "res_order" => $res_order,
        ));
    }

    public function edit_post(){
        $id=$this->input->get_post('id')?$this->input->get_post('id'):'';
        if(empty($id)){
            showMessage('index.php?c=post&m=index',-1,'参数错误');
        }
        $post_data=$this->PostModel->query_one(array('id'=>$id));
        if($post_data['audio_id']){
            $audio_data=$this->AudioModel->query_one(array('id'=>$post_data['audio_id']));
        }
        $pageTitle = "编辑";
        $this->load->view('post/edit',array(
            "data" => $post_data,
            "audio_data" => $audio_data,
            "pageTitle" => $pageTitle,
        ));

    }


    /**
     * 恢复删除的帖子
     * name: recovery_post
     * @Author:taiyang
     * @time:2018-04-17 16:50:56 add taiyang
     */
    public function recovery_post(){
        $post_id=$this->input->get_post('id')?$this->input->get_post('id'):0;
        $desc=$this->input->get_post('desc')?$this->input->get_post('desc'):'';
        if($post_id){
            $res=$this->PostModel->update_data(array('status'=>1,'is_audit'=>0),array('id'=>$post_id));
            if($res !== false){
                //查询帖子的uid
                $post=$this->PostModel->query_one(array('id'=>$post_id));
                // 修改用户统计   帖子数-1
                $this->UserCount->db->set('pub_num','pub_num+1',false)->where(array('uid'=>$post['uid']));
                $this->UserCount->db->update('t_user_count');
            }
            //添加修改人信息
            $this->AdminOpenationModel->insertLog($post_id,25,!empty($desc)?$desc:'恢复删除的帖子，帖子id:'.$post_id);
            echo json_encode(array('status'=>1));exit;

        }
        echo json_encode(array('status'=>2));exit;
    }

    /**
     * 审核一个帖子
     * name: update_post
     * @throws Exception
     * @Author:taiyang
     */
    public function update_post(){
        $is_hot=$this->input->get_post('is_hot')?$this->input->get_post('is_hot'):0;
        $post_id=$this->input->get_post('id')?$this->input->get_post('id'):0;
        $tag=$this->input->get_post('tag')?$this->input->get_post('tag'):'';//假设类型为array
        $topic=$this->input->get_post('topic')?$this->input->get_post('topic'):'';//假设类型为array
        if(empty($post_id) || empty($tag) || empty($topic)){
            echo json_encode(array('status'=>2,'msg'=>'话题、标签不能为空'));exit;

        }
        //获取帖子用户信息
        $post_data=$this->PostModel->query_one(array('id'=>$post_id));
        if($is_hot == 1){//审核前判断图片是否符合标准
            //查询图片信息
            if($post_data['pic_id']<1){
                echo json_encode(array('status'=>2,'msg'=>'加精帖子需要为帖子上传封面图片！'));exit;
            }
            $img_data=$this->ImgModel->query_one(array('id'=>$post_data['pic_id']));
            if($img_data['w']<720 ||$img_data['h']<720 ||$img_data['size']<20480){
                echo json_encode(array('status'=>2,'msg'=>'请检查图片的宽高和大小是否符合标准！或换一张图片'));exit;
            }

        }
        //查询分类话题
        $topic_id=$this->PostTopicModel->get_post_topic($post_id);
        $tag_id=$this->PostTagModel->get_post_tag($post_id);
        if($topic){//写入话题
            //移除话题
            if(!empty($topic_id['ids'])){
                foreach ($topic_id['ids'] as $t_id){
                    $this->PostTopicModel->del_add_post_topic($post_id,$t_id,2);
                }
            }
            //添加话题
            foreach ($topic as $id){
                $this->PostTopicModel->del_add_post_topic($post_id,$id,1);
            }
        }
        if($tag){//写入标签
            //移除标签
            if($tag_id['ids']){
                foreach ($tag_id['ids'] as $t_id){
                    $this->PostTagModel->del_add_post_tag($post_id,$t_id,2);
                }
            }
            //添加标签
            foreach ($tag as $id){
                $this->PostTagModel->del_add_post_tag($post_id,$id,1);
            }
        }
        //修改POST
        $this->PostModel->update_data(array('is_hot'=>$is_hot,'status'=>1,'is_audit'=>1),array('id'=>$post_id));
        //添加修改人信息
        $this->AdminOpenationModel->insertLog($post_id,1,'审核帖子通过，帖子id:'.$post_id);
        $topic_arr=$this->PostTopicModel->get_post_topic($post_id);
        //task-event
        if($is_hot == 1){
            //查询是否发过一次精华通知
            $is_report=$this->NoticeModel->query_one(array('related_type'=>2,'related_id'=>$post_id,'type'=>1));
            if(empty($is_report)){
                $login = getLoginUser();
                //发通知
                $this->NoticeModel->send_notice($post_data['uid'], $login['uid'], 1, 1, $post_id);
            }
            //触发任务埋点
            $this->PostModel->taskEventApi('post_essence',array('uid'=>$post_data['uid'],'data'=>'post_essence:'.$post_id));
        }
        //ajax或跳转
        echo json_encode(array('status'=>1,'class_title'=>$topic_arr['class'],'top_ids'=>implode(',',$topic),'tag_ids'=>implode(',',$tag),'msg'=>'审核成功'));exit;

    }


    /**
     * 删除一个帖子
     * name: del_post
     * @throws Exception
     * @Author:taiyang
     */
    public function del_post(){
        $post_id=$this->input->get_post('id')?$this->input->get_post('id'):0;
        $desc=$this->input->get_post('desc')?$this->input->get_post('desc'):'';
        $tag_id=$this->input->get_post('tag_id')?$this->input->get_post('tag_id'):'';
        if($post_id){
            $reportTag = $this->ReportModel->tag;
            //删除逻辑
            $this->PostModel->del_post($post_id);
            //取出帖子信息
            $post_data=$this->PostModel->query_one(array('id'=>$post_id));
            $userData = getLoginUser();
            //通知
            $this->NoticeModel->send_notice($post_data['uid'], $userData['uid'], 2, 1, $post_id, $reportTag[$tag_id], '');
            //添加修改人信息
            $this->AdminOpenationModel->insertLog($post_id,2,!empty($desc)?$desc:'审核删除帖子，帖子id:'.$post_id.'['.$reportTag[$tag_id].']');
            echo json_encode(array('status'=>1));exit;

        }
        echo json_encode(array('status'=>2));exit;
    }

    /**
     * 把帖子标记为审核不通过
     * name: up_post
     * @Author:taiyang
     */
    public function up_post(){
        $post_id=$this->input->get_post('id')?$this->input->get_post('id'):0;
        $desc=$this->input->get_post('desc')?$this->input->get_post('desc'):'';
        if($post_id){
            $post=$this->PostModel->query_one(array('id'=>$post_id));
            $res=$this->PostModel->update_data(array('status'=>2,'is_audit'=>1),array('id'=>$post_id));
            //task-event
            //触发任务埋点
            $this->PostModel->taskEventApi('check_post_false',array('uid'=>$post['uid'],'data'=>'check_post_false:'.$post_id));
//            if($res !== false){
//                // 修改用户统计   帖子数-1
//                $this->UserCount->db->set('pub_num','pub_num-1',false)->where(array('uid'=>$post['uid']));
//                $this->UserCount->db->update('t_user_count');
//            }
            //添加修改人信息
            $this->AdminOpenationModel->insertLog($post_id,3,!empty($desc)?$desc:'审核不通过，帖子id'.$post_id);
            echo json_encode(array('status'=>1));exit;

        }
        echo json_encode(array('status'=>2));exit;


    }

    /**
     * 审核时弹窗页面
     * name: post_check
     * @Author:taiyang
     */
    public function post_check(){
        $post_id=$this->input->get_post('id')?$this->input->get_post('id'):'';
        $type=$this->input->get_post('type')?$this->input->get_post('type'):1;//1审核不通过2删除
        if(empty($post_id)){
            echo json_encode(array('status'=>2));exit;
        }

        $pageTitle = $type == 2?"删除帖子":($type == 3?'恢复删除帖子':"审核帖子不通过");
        $this->load->view('post/post_check',array(
            "type" => $type,
            "post_id" => $post_id,
            "pageTitle" => $pageTitle,
        ));


    }

    /**
     * 审核时更新图片
     * name: ajax_update_pic
     * @throws Exception
     * @Author:taiyang
     */
    public function ajax_update_pic(){
        $post_id=$this->input->get_post('id')?$this->input->get_post('id'):0;
        $img_id=$this->input->get_post('img_id')?$this->input->get_post('img_id'):0;
        if(empty($post_id) || empty($img_id)){
            echo json_encode(array('status'=>2));exit;
        }
//        $pic_path=$this->input->get_post('path')?$this->input->get_post('path'):'';

        //去掉原有图片的关联数据
        $this->ImgModel->update_data(array('related_id'=>0),array('related_id'=>$post_id,'type'=>1));
        //把新的图片关联到现在的帖子
        $this->ImgModel->update_data(array('related_id'=>$post_id),array('id'=>$img_id));
        //修改帖子图片id
        $this->PostModel->update_data(array('pic_id'=>$img_id,'pics'=>$img_id),array('id'=>$post_id));
        echo json_encode(array('status'=>1));exit;

    }


    public function add_tag(){
        $title=$this->input->get_post('title')?$this->input->get_post('title'):'';
        $select_name=$this->input->get_post('select_name')?$this->input->get_post('select_name'):'';
        if(empty($title) || empty($select_name)){
            echo json_encode(array('status'=>2));exit;
        }
        if($select_name == 'topic_list[]'){
            //话题
            //取出所有的话题
            $topic_list=$this->TopicModel->getTopicList();
            $id=$this->TopicModel->insert_one(array('title'=>$title,'parent_id'=>199,'level'=>2));
            $topic_list[]=array('id'=>$id,'title'=>$title);
            echo json_encode(array('status'=>1,'json_arr'=>json_encode($topic_list),'type'=>'top','id'=>$id));exit;
        }elseif ($select_name == 'tag_list[]'){
            //标签
            //取出所有的标签
            $tag_list=$this->TagModel->getTagList();
            $id=$this->TagModel->insert_one(array('title'=>$title,'parent_id'=>5,'level'=>2));
            $tag_list[]=array('id'=>$id,'title'=>$title);
            echo json_encode(array('status'=>1,'json_arr'=>json_encode($tag_list),'type'=>'tag','id'=>$id));exit;
        }

    }
    public function up_title(){
        $id=$this->input->get_post('id')?$this->input->get_post('id'):'';
        $title=$this->input->get_post('title')?$this->input->get_post('title'):'';
        if(empty($id) || empty($title)){
            echo json_encode(array('status'=>-1,'msg'=>'帖子标题不能为空……'));exit;
        }
        $this->PostModel->update_data(array('title'=>$title),array('id'=>$id));
        //添加修改人信息
        $this->AdminOpenationModel->insertLog($id,26,'修改帖子标题，帖子id:'.$id);
        echo json_encode(array('status'=>1,'msg'=>'修改成功！'));exit;
    }

    public function get_like_post(){
        $name = $this->input->get_post('tag_name') ? $this->input->get_post('tag_name') : '';
        $where=array(
            's_type_id'=>3,
            's_content'=>$name,
        );
        $list=$this->PostModel->getPostAll($where,1,100);
        std_out(0,'',$list);
    }

}